import {defineStore} from 'pinia'
import {ListUserTool, PopEditUserToolType} from './consts.js'
import {reactive} from "vue";
import {dbdto, dto} from "../../wailsjs/go/models";
import {CreateUserTool, DelUSerTool, GetListUserTool, UpdateUserTool} from "../../wailsjs/go/dbservice/UserToolService";
import {DelConfirm, ToastError, ToastInfo} from "../utils/Toast";
import {LangTypeCn} from "../constant/enums/lang_type";
import {ClipboardGetText, ClipboardSetText} from "../../wailsjs/runtime";
export enum EditType {
    Add,
    Update,
    Delete ,
}
export const useIndexStore = defineStore({
    id: 'index',
    state: () => ({
       // IsLogin:false,//当前是否已登录
        //弹窗相关的store
        PopEditUserTool: reactive<PopEditUserToolType>({
            CurrEditType: EditType.Add,
            CurrFormData: new dbdto.UserToolAdd({}),
            IsShow: false
        }),
        //列表相关的
        ListUserTool: reactive<ListUserTool>({
            Data: []
        }),
        //首页全局
        Index: {
            SearchText:"",
        },
        App:{
            ShowPop:false,

        },
        Lang:{
            CurrLangType:LangTypeCn,
            LangDataDic:reactive< Record<string, string>>({}),
        }

    }),
    actions: {
        //显示新增弹窗
        ShowUserToolAdd() {
           this.SetCurrEditType(EditType.Add)
            this.PopEditUserTool.IsShow = true
            this.PopEditUserTool.CurrFormData = new dbdto.UserToolAdd({
                tool_name: '',
                tool_icon: '',
                tool_icon_color: '#3d7ee0', // 图标颜色的十六进制表示法
                prompt: '',
                hot_key:"",
                use_hot_key:false,
                complete_do:"",
                quick_pos:0,
                input_count: 0 // 输入框数量
            })
        },
        // 显示编辑弹窗 formData要包含id
        ShowUserToolUpdate(formData: any) {
            this.SetCurrEditType(EditType.Update)
            console.log("ShowUserToolUpdate formData",formData)
            this.PopEditUserTool.IsShow = true
            if(!formData.input_count){
                formData.input_count=0
            }
            this.PopEditUserTool.CurrFormData = formData
            console.log(" this.PopEditUserTool.CurrEditType", this.PopEditUserTool.CurrEditType)
        },
        SetCurrEditType(typ : EditType){
            this.PopEditUserTool.CurrEditType = typ
        },
        //保存数据到数据库
        SaveUserToolFormData() {
            if (this.PopEditUserTool.CurrEditType == EditType.Add) {//新增
                CreateUserTool(this.PopEditUserTool.CurrFormData).then((res: any) => {
                    res.code == 1 ? ToastInfo(res.msg) : ToastError(res.msg)
                    this.PopEditUserTool.IsShow = res.code != 1
                    this.GetList()
                })
            } else {
                UpdateUserTool(this.PopEditUserTool.CurrFormData).then((res: any) => {
                    res.code == 1 ? ToastInfo(res.msg) : ToastError(res.msg)
                    this.PopEditUserTool.IsShow = res.code != 1
                    this.GetList()
                })
            }
        },
        //删除用户工具
        DelUserTool(id:any){
            DelConfirm("确定删除么？删除后不可恢复！！").then((askFlag:boolean)=>{
                if(askFlag){ //用户点击确认删除
                    DelUSerTool(id).then((res:dto.RespData)=>{
                        this.GetList()
                        res.code == 1 ? ToastInfo(res.msg) : ToastError(res.msg)
                    })
                }
            })
        },
        CopyUserTool(data:any){
            let DataStr=JSON.stringify(data)
            ClipboardSetText(DataStr)
            ToastInfo("应用信息已经复制到剪贴板,可直接将信息分享应用给好友")
        },
        ParseUserTool(){
             ClipboardGetText().then((dataStr:string)=>{
                 try {
                 let data= JSON.parse(dataStr) as dbdto.UserToolAdd
                  console.log(data," ---data")
                 if(!data  || !data.tool_name){
                     ToastError("剪贴板文本是无效的应用信息！")
                     return
                 }
                console.log(dataStr," ---dataStr")
                CreateUserTool(data).then((res: any) => {
                    if( res.code == 1){
                        ToastInfo("应用通过剪贴板创建完成")
                    }else{
                        ToastError(res.msg)
                    }
                 })

                 }catch (e){
                     ToastError("剪贴板文本是无效的应用信息！ "+(e as Error).message)
                 }

             })

        },
        //关闭弹窗
        CloseUserToolPopup() {
            this.PopEditUserTool.IsShow = false
            this.GetList()
        },
        //获取工具列表
        GetList() {
            GetListUserTool().then((res: dto.RespData) => {
                console.log(res)
                if (res.code == 1) this.ListUserTool.Data = res.data
            })
        }
    },
})